$(function() {
	//得到焦点
	$("#pwd").focus(function() {
		$("#left_hand").animate({
			left: "150",
			top: " -38"
		}, {
			step: function() {
				if(parseInt($("#left_hand").css("left")) > 140) {
					$("#left_hand").attr("class", "left_hand");
				}
			}
		}, 2000);
		$("#right_hand").animate({
			right: "-64",
			top: "-38px"
		}, {
			step: function() {
				if(parseInt($("#right_hand").css("right")) > -70) {
					$("#right_hand").attr("class", "right_hand");
				}
			}
		}, 2000);
	});
	//失去焦点
	$("#password").blur(function() {
		$("#left_hand").attr("class", "initial_left_hand");
		$("#left_hand").attr("style", "left:100px;top:-12px;");
		$("#right_hand").attr("class", "initial_right_hand");
		$("#right_hand").attr("style", "right:-112px;top:-12px");
	});

    //回车键的搜索功能
    $('#pwd').keydown(function (e) {
        if (e.keyCode == 13) {
            box_login();
        }
    });

});

//强行更改登录密码
function modifyPasswordWin() {
	ComOpenWin(1, "第一次登录强制修改密码", "600px", "auto", $('#modifyPasswordWin'), 1);
	$(".layui-layer-shade").css("height", "0px");
}

//登录
function box_login(){
	var username = $('#user').val().trim();
	var password = $('#pwd').val().trim();
	var objChk = document.getElementById("chkRememberPass");
	if (username == "") {
		$("#user").addClass("error");
		$("#user_span").css("display","block");
		$("#user_span").html("请输入用户名");
	} if (password == "") {
		$("#pwd").addClass("error");
		$("#pass_span").css("display","block");
		$("#pass_span").html("请输入密码");
		$(".context_top").css("height","300px");
	}else{

        $("#user_span,#pass_span").css("display","none");
        $("#pwd,#user").removeClass("error");
        $.post("/login", {
            "username": username,
            "password": password
        }, function (value) {
        	console.info(value);
            if (value.status == 0){
            	var data = value.data;
            	$("#modifyPasswor_userName").html(data.userName); //用户名
				$("#modifyPasswor_userId").val(data.userEncryptid); //用户加密id
            	if(data.userFirst == "0"){ //首次登录
                    modifyPasswordWin();
				}else {
                    setcookie("token",data.userName,1,"/");//用户名
                    setcookie("uid",data.userId,1,"/"); //用户id
                    setcookie("uid_encryptid",data.userEncryptid,1,"/"); //用户加密id
                    setcookie("admin_role",data.userRole,1,"/"); //登录人的角色编号 0：普通用户，1：商家，2：超级管理员
                    window.location.href="index.html";
				}
            }else{
            	if(value.status == 40002){
                    $("#user").addClass("error");
                    $("#user_span").css("display","block");
                    $("#user_span").html(value.msg);
                    $(".context_top").css("height","280px");
				}else {
                    $("#pwd").addClass("error");
                    $("#pass_span").css("display","block");
                    $("#pass_span").html(value.msg);
                    $(".context_top").css("height","280px");
				}
            }
        });
	}
}

//用户强制更改密码
function  forceModifyPassword(){
    debugger;
    var newpwd1 = $("#newPassword").val().trim();
    var newpwd2 = $("#newPassword2").val().trim();
    var reg = new RegExp(/^[A-Za-z].*[0-9]|[0-9].*[A-Za-z]|[0-9].*[!@#$%^&*,.?]|[A-Za-z].*[!@#$%^&*,.?]|[!@#$%^&*,.?].*[A-Za-z]|[!@#$%^&*,.?].*[0-9]$/); //必须包含字母、数字、符号任意二项
    if($("#newPassword").val().trim()!=$("#newPassword2").val().trim()){
        layer.msg('两次输入密码不一样，请重新输入', {icon: 0,time:4000});
        $("#newPassword").val("");
        $("#newPassword2").val("");
    }else if(!((newpwd1.length>=6 && newpwd1.length<=16) && (newpwd2.length>=6 && newpwd2.length<=16))) {
        layer.msg('密码必须是由6-16个字符组成且至少包含字母、数字、符号任意二项', {icon: 0,time:4000});
        $("#newPassword").val("");
        $("#newPassword2").val("");
    }else if(!reg.test(newpwd2) && !reg.test(newpwd1)){
        layer.msg('密码必须是由6-16个字符组成且至少包含字母、数字、符号任意二项', {icon: 0,time:4000});
    }else {
        var layLoad=layer.load(2);
        var userId=$("#modifyPasswor_userId").attr("value"); //用户加密id
        var oldPwd=$("#oldPassword").val().trim();
        var newPwd=$("#newPassword").val().trim();
        $.post("/forceModifyPassword", {
            "userEncryptid": userId,
            "oldPwd": oldPwd,
            "newPwd": newPwd
        }, function (value) {
            layer.close(layLoad);
            if(value.status==0){
                mclose();
                layer.msg('修改密码成功,请重新登录！', {icon: 1,time:2000});
                $('#pwd').val("");
            }else{
                if(value=="" || value==null){
                    layer.alert("接口出现错误！", {icon: 2});
                }else {
                    layer.alert(value.msg, {icon: 2});
                }
            }
        })
    }
}